﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Bussiness;


namespace GUI
{
    public partial class frmPhieuThuTien : Form
    {
        private List<KhachHangDTO> _khachHangList;
        
        public frmPhieuThuTien()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e) {
            Close();
        }

        private void frmPhieuThuTien_Load(object sender, EventArgs e) {
            //lay danh sach khach hang
            KhachHangService khService = new KhachHangService();
            _khachHangList = khService.GetKhachHangList();

            //gan len cobobox
            cbxMaKhachHang.DataSource = this._khachHangList;
            cbxMaKhachHang.DisplayMember = KhachHangColumn.MaKhachHang;
            cbxMaKhachHang.ValueMember = KhachHangColumn.IDKhachHang;           
            

            //tao ma phieu thu tu dong
            PhieuThuTienService phieuThuSer = new PhieuThuTienService();
            tbxMaPhieuThu.Text = phieuThuSer.CreateMaPhieuThu();
        }

        private void cbxMaKhachHang_SelectedIndexChanged(object sender, EventArgs e) {
            //gan thong tin khach hang len cac text box tuong ung
            KhachHangDTO dto = _khachHangList[cbxMaKhachHang.SelectedIndex];
            tbxDiaChi.Text = dto.DiaChi;
            tbxDienThoai.Text = dto.DienThoai;
            tbxEmail.Text = dto.Email;
            tbxTenKhachHang.Text = dto.HoTen;
            tbxTienNo.Text = dto.TienNo.ToString();
        }

        private void tbxSoTienThu_KeyPress(object sender, KeyPressEventArgs e) {

        }

        private void btnLuu_Click(object sender, EventArgs e) {
            if (txtSoTienThu.Text == "") {
                MessageBox.Show("chưa nhập số tiền");
                txtSoTienThu.Focus();
                return;
            }

            //luu phieu thu
            PhieuThuTienService service = new PhieuThuTienService();
            PhieuThuTienDTO phieuThu = new PhieuThuTienDTO();
            KhachHangDTO khachHang = _khachHangList[cbxMaKhachHang.SelectedIndex];

            phieuThu.IDKhachHang = _khachHangList[cbxMaKhachHang.SelectedIndex].IDKhachHang;
            phieuThu.MaPhieuThu = tbxMaPhieuThu.Text;
            phieuThu.NgayThu = dateNgayThu.Value;
            phieuThu.SoTien = Convert.ToInt32(txtSoTienThu.Text);

            string message = "";
            service.InsertNewPhieuThu(phieuThu, khachHang, ref message);
            MessageBox.Show(message);
        }

        private void txtSoTienThu_KeyPress(object sender, KeyPressEventArgs e) {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar)) {
                e.Handled = true;
            }
        }
    }
}
